package cn.zl.web.impl.streampush;

import cn.yunbamboos.exception.AppException;
import cn.yunbamboos.id.SnowflakeIdGenerator;
import cn.yunbamboos.rest.service.anno.RestServiceMapping;
import cn.yunbamboos.rest.service.dto.Page;
import cn.yunbamboos.rest.service.dto.out.BaseOutDTO;
import cn.yunbamboos.rest.service.transaction.Transaction;
import cn.zl.common.manage.StreamManage;
import cn.yunbamboos.mybatis.conditions.Conditions;
import cn.zl.config.ZLConfig;
import cn.zl.model.StreamPush;
import cn.zl.web.dto.streampush.AddStreamPushInDTO;
import cn.zl.web.dto.streampush.AddStreamPushOutDTO;
import cn.zl.web.dto.streampush.DeleteStreamPushInDTO;
import cn.zl.web.dto.streampush.DeleteStreamPushOutDTO;
import cn.zl.web.dto.streampush.PlayStreamPushInDTO;
import cn.zl.web.dto.streampush.PlayStreamPushOutDTO;
import cn.zl.web.dto.streampush.QueryStreamPushByPageInDTO;
import cn.zl.web.dto.streampush.QueryStreamPushByPageOutDTO;
import cn.zl.web.dto.streampush.UpdateStreamPushInDTO;
import cn.zl.web.dto.streampush.UpdateStreamPushOutDTO;
import cn.zl.web.impl.StreamService;
import cn.zl.web.inter.streampush.IStreamPushService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service("StreamPushServiceImpl")
public class StreamPushServiceImpl extends StreamService implements IStreamPushService {

    @Resource
    private SnowflakeIdGenerator snowflakeIdGenerator;
    @Resource
    private StreamManage streamManage;
    @Resource
    private ZLConfig zlConfig;

    @RestServiceMapping(url = "/web/stream-push/queryByPage", name = "条件分页查询推流列表服务", in = QueryStreamPushByPageInDTO.class, out = QueryStreamPushByPageOutDTO.class)
    @Override
    public BaseOutDTO queryByPage(QueryStreamPushByPageInDTO in) {
        // 创建分页对象
        Page<StreamPush> page = new Page<>(in.getPageNo(), in.getPageSize());
        // 添加动态条件
        Conditions conditions = new Conditions();
        // 查询总数
        int total = streamPushMapper.countForPage(page, conditions);
        // 查询当前页码数据
        List<StreamPush> list = streamPushMapper.queryForPage(page, conditions);
        // 处理性别 用户状态 角色
        page.setRows(list).setTotal(total);
        return new QueryStreamPushByPageOutDTO(page);
    }

    @RestServiceMapping(url = "/web/stream-push/add", name = "添加新的推流信息服务", in = AddStreamPushInDTO.class, out = AddStreamPushOutDTO.class, transaction = Transaction.WRITE)
    @Override
    public BaseOutDTO add(AddStreamPushInDTO in) {
        StreamPush push = in.getStreamPush();
        this.checkAppAndStream(push.getApp(), push.getStream());
        push.setPushId(snowflakeIdGenerator.nextIdStr());
        LocalDateTime now = LocalDateTime.now();
        push.setUpdateTime(now);
        push.setCreateTime(now);
        streamPushMapper.insert(push);
        return new AddStreamPushOutDTO();
    }

    @RestServiceMapping(url = "/web/stream-push/update", name = "更新推流信息服务", in = UpdateStreamPushInDTO.class, out = UpdateStreamPushOutDTO.class, transaction = Transaction.WRITE)
    @Override
    public BaseOutDTO update(UpdateStreamPushInDTO in) {
        StreamPush streamPush = in.getStreamPush();
        LocalDateTime now = LocalDateTime.now();
        streamPush.setCreateTime(now);
        streamPushMapper.update(streamPush);
        return new UpdateStreamPushOutDTO();
    }

    @RestServiceMapping(url = "/web/stream-push/delete", name = "删除推流信息服务", in = DeleteStreamPushInDTO.class, out = DeleteStreamPushOutDTO.class, transaction = Transaction.WRITE)
    @Override
    public BaseOutDTO delete(DeleteStreamPushInDTO in) {
        in.forEach(id -> {
            streamPushMapper.deleteByPrimaryKey(id);
        });
        return new DeleteStreamPushOutDTO();
    }

    @RestServiceMapping(url = "/web/stream-push/play-stream", name = "播放推流视频服务", in = PlayStreamPushInDTO.class, out = PlayStreamPushOutDTO.class, transaction = Transaction.WRITE)
    @Override
    public BaseOutDTO playStream(PlayStreamPushInDTO in) {
        String realUrl = in.getSession("real_url");
        URI url = URI.create(realUrl);
        String host = url.getHost();
        PlayStreamPushOutDTO out = new PlayStreamPushOutDTO();
        String pushId = in.getPushId();
        Optional<StreamPush> optional = streamPushMapper.queryByPrimaryKey(pushId);
        optional.ifPresent(stream -> {
            int state = streamManage.getState(stream.getApp(), stream.getStream());
            if (state != 1) {
                throw AppException.c("推流未开启，请先开启在播放");
            }
            out.setHost(host);
            out.setApp(stream.getApp());
            out.setStream(stream.getStream());
            out.setConfig(zlConfig);
        });
        return out;
    }
}
